import $store from '@/store/index.js';
import friendsAppSqlite from "@/sqlite_services/friendsApp.js"
//好友申请
const handle = async(data)=>{
	try{
		console.log('来了');
		let myid = $store.state.user.user_info.user_id
		if(data.recipient_id == myid && data.req_status == 'pending'){
			
			//这里代表有人添加我为好友
			uni.showToast({
				title:`${data.requester_nick_name}${getApp().$t("addressbook.friend_apply.addFriend")}`,
				icon:'none'
			})
			//未读数+1
		    $store.dispatch('updateFriendBadge',{ type: 'add', count:1 })
		    //添加到数据库
			friendsAppSqlite.addOrEditFrindAppInfo({
				apply_id:data.apply_id,
				friend_id:data.requester_id,
				isMe:'0',
				message:data.message,
				req_status:data.req_status
			})
			//通知页面
			uni.$emit('onMessageFriendApp', { 
				type:'pending',
				data
			})
		}else if(data.requester_id == myid && data.req_status == 'pending'){
			//这里代表我添加别人为好友,只需要存数据库
			//添加到数据库
			friendsAppSqlite.addOrEditFrindAppInfo({
				apply_id:data.apply_id,
				friend_id:data.recipient_id,
				isMe:'1',
				message:data.message,
				req_status:data.req_status
			})
		}else if(data.recipient_id == myid && data.req_status == 'rejected'){
			//我拒绝了别人
			//未读数-1
			$store.dispatch('updateFriendBadge',{ type: 'sub', count:1 })
			//添加到数据库
			friendsAppSqlite.addOrEditFrindAppInfo({
				apply_id:data.apply_id,
				friend_id:data.requester_id,
				isMe:'0',
				message:data.message,
				req_status:data.req_status
			})
			//通知页面
			uni.$emit('onMessageFriendApp', {
				type:'rejected',
				data
			})
		}else if(data.requester_id == myid && data.req_status == 'rejected'){
			//别人拒绝了我
			uni.showToast({
				title:`${data.recipient_nick_name}${getApp().$t("addressbook.friend_apply.refuseAddFriend")}`,
				icon:'none'
			})
			//添加到数据库，代表我添加的，保留
			friendsAppSqlite.addOrEditFrindAppInfo({
				apply_id:data.apply_id,
				friend_id:data.recipient_id,
				isMe:'1',
				message:data.message,
				req_status:data.req_status
			})
		}else if(data.recipient_id == myid && data.req_status == 'accepted'){
			//我通过了别人
			//未读数-1
			$store.dispatch('updateFriendBadge',{ type: 'sub', count:1 })
			//添加到数据库
			friendsAppSqlite.addOrEditFrindAppInfo({
				apply_id:data.apply_id,
				friend_id:data.requester_id,
				isMe:'0',
				message:data.message,
				req_status:data.req_status
			})
			
			//好友列表添加
			$store.dispatch('addFriendList',{
				friend_idx:data.requester_id,
				nick_name:data.requester_nick_name,
				avatar_url:data.requester_avatar_url
			})
			
			//通知页面
			uni.$emit('onMessageFriendApp', {
				type:'accepted',
				data
			})
		}else if(data.requester_id == myid && data.req_status == 'accepted'){
			//别人通过了我
			uni.showToast({
				title:`${data.recipient_nick_name}${getApp().$t("addressbook.friend_apply.agreeAddFriend")}`,
				icon:'none'
			})
			//别人通过我之后通知一下页面
			uni.$emit('onMessageFriendApp', {
				type:'accepted',
				data
			})
			//好友列表添加别人
			$store.dispatch('addFriendList',{
				friend_idx:data.recipient_id,
				nick_name:data.recipient_nick_name,
				avatar_url:data.recipient_avatar_url
			})
			//添加到数据库
			friendsAppSqlite.addOrEditFrindAppInfo({
				apply_id:data.apply_id,
				friend_id:data.recipient_id,
				isMe:'1',
				message:data.message,
				req_status:data.req_status
			})
		}
	}catch(e){
		
	}
}
export default handle